var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
var hasclick = false;
var lastTime = null;
Page({
  data: {
    orderId: '',
    orderList: [],
    ordertype: 0,
    page: 1,
    limit: 10
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.ordertype) {
      this.setData({
        ordertype: options.ordertype
      });
    }
    this.setData({
      orderList: [],
      page: 1,
      limit: 10
    });
    if (hasclick) {
      return
    }
    hasclick = true;
    this.getOrderList();
  },

  //获取订单数据
  getOrderList() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    util.request(api.MyOrderList, {
      ordertype: that.data.ordertype,
      page: that.data.page,
      limit: that.data.limit
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.length > 0) {
          that.setData({
            orderList: that.data.orderList.concat(res.data),
            page: that.data.page + 1
          });
        }
        hasclick = false;
        wx.hideLoading();
      }
    });
  },

  //下拉触底事件
  onReachBottom() {
    this.getOrderList();
  },

  //切换页签事件
  switchTab: function (event) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    if (hasclick) {
      return
    }
    hasclick = true;
    let ordertype = event.currentTarget.dataset.ordertype;
    that.setData({
      orderList: [],
      ordertype: ordertype,
      page: 1,
      limit: 10
    });
    that.getOrderList();
  },

  // “去付款”按钮点击效果
  payOrder: function (e) {
    util.jhxLoadShow("正在支付，请稍后...");
    let nowTime = +new Date();
    if (nowTime - lastTime > 5000 || !lastTime) { //5秒内避免重复提交订单
      lastTime = nowTime;
    } else {
      return false;
    }
    let that = this;
    let orderId = e.currentTarget.dataset.index;
    that.setData({
      orderId: orderId
    });

    let payAmount = e.currentTarget.dataset.payamount;
    let memberUserid = wx.getStorageSync('memberUserid');
    util.request(api.OrderPrepay, {
      memberUserid: memberUserid,
      orderId: orderId,
      amount_total: payAmount
    }, 'POST').then(function (res) {
      util.jhxLoadHide();
      if (res.result == 1) {
        const payParam = res.data;
        console.log("支付过程开始");
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log("支付过程成功");
            wx.redirectTo({
              url: '/pages/payResult/payResult?status=1&orderId=' + that.data.orderId
            });
          },
          'fail': function (res) {
            console.log("支付过程失败");
          },
          'complete': function (res) {
            console.log("支付过程结束");
            wx.requestSubscribeMessage({
              tmplIds: app.globalData.tmplIds,
              success(res) {
                util.request(api.Ordermessage, {
                  orderId: that.data.orderId
                }, 'POST').then(function (res) {
                });
              }
            })
          }
        });
      } else {
        wx.redirectTo({
          url: '/pages/payResult/payResult?status=0&orderId=' + that.data.orderId
        });
      }
    });

  },
  // “取消订单”点击效果
  cancelOrder: function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.index;
    wx.showModal({
      title: '',
      content: '确定要取消此订单？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderCancel, {
            orderId: orderId
          }).then(function (res) {
            if (res.result == 1) {
              wx.showToast({
                title: '取消订单成功！'
              });
              let _orderList = that.data.orderList;
              for (let i = 0; i < _orderList.length; i++) {
                if (_orderList[i].id == orderId) {
                  _orderList.splice(i, 1)
                }
              }
              that.setData({
                orderList: _orderList
              });
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },

  // “确认收货”点击效果
  confirmOrder: function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.index;

    wx.showModal({
      title: '',
      content: '确认收货？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderConfirm, {
            orderId: orderId
          }).then(function (res) {
            if (res.result == 1) {
              wx.showToast({
                title: '确认收货成功！'
              });
              let _orderList = that.data.orderList;
              for (let i = 0; i < _orderList.length; i++) {
                if (_orderList[i].id == orderId) {
                  _orderList.splice(i, 1)
                }
              }
              that.setData({
                orderList: _orderList
              });
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  detailExpress: function (e) {
    let orderId = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/ucenter/expressInfo/expressInfo?orderId=' + orderId
    });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})